package com.lysj.admin.master.common.spring.mpbase;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.spring.base.BaseContent;
import com.lysj.admin.master.common.spring.pagination.PageRequestDTO;
import com.lysj.admin.master.common.validation.Validation;
import com.lysj.admin.master.common.validation.domain.BindingResult;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;


/**
 * @author Created by zk on 2019-12-24 15:52
 * @description
 */
@Slf4j
public abstract class MyController<M extends BaseMapper<E>, E extends IdEntity, T extends PageRequestDTO> extends BaseContent {
    public abstract MyService<M, E> getService();

    /**
     * @author zk
     * @Description 单表多条件分页查询 此处delFlag 默认为 1
     * @date 2018-07-19 15:29
     */
//    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    @ResponseBody
    public Resp list(T condition) {
        return RespUtil.success(getService().pageList(condition));
    }

    /**
     * @author zk
     * @Description 添加数据
     * @date 2018-07-19 15:31
     */
//    @RequestMapping(value = "/save", method = RequestMethod.POST)
//    @ResponseBody
    public Resp save(E entity) {
        //对实体类中的@validation注解进行校验
        BindingResult bindingResult = Validation.valid(entity);
        if (!bindingResult.isFlag()) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, bindingResult.getMessage().get(0));
        }
        getService().save(entity);
        return RespUtil.success("添加成功");
    }

    /**
     * @author zk
     * @Description 更新数据
     * @date 2018-07-19 15:32
     */
//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    @ResponseBody
    public Resp update(E entity) {
        if (ParamUtil.isBlank(entity.getId())) {
            log.error("The entity id is blank->e:{}", entity);
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "Id will not be blank");
        }
        E e = getService().getById(entity.getId());
        if (e == null) {
            log.error("The entity is not exist->e:{}", entity);
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR, "This id is not exist," + entity.getId());
        }
        //获取Hutool拷贝实例
        CopyOptions copyOptions = CopyOptions.create();
        //忽略为null值得属性
        copyOptions.setIgnoreNullValue(true);
        //进行属性拷贝
        BeanUtil.copyProperties(entity, e, copyOptions);
        //对实体类中的@validation注解进行校验
        BindingResult bindingResult = Validation.valid(e);
        if (!bindingResult.isFlag()) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, bindingResult.getMessage().get(0));
        }
        getService().updateById(e);
        return RespUtil.success("修改成功");
    }

    /**
     * @author zk
     * @Description 通过ids批量删除 此处为物理删除
     * @date 2018-07-19 15:34
     */
//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    @ResponseBody
    public Resp delete(String[] ids) {
        getService().removeByIds(Arrays.asList(ids));
        return RespUtil.success("删除成功");
    }

    //    @RequestMapping(value = "/logic_delete", method = RequestMethod.POST)
//    @ResponseBody
    public Resp logicDelete(String[] ids) {
        UpdateWrapper<E> wrapper = new UpdateWrapper<>();
        wrapper.in(CommonConstant.ID_NAME,Arrays.asList(ids));
        wrapper.set(CommonConstant.DEL_FLAG_NAME,CommonConstant.DEL_FLAG);
        getService().update(wrapper);
        return RespUtil.success("删除成功");
    }
}
